<?php

class AdminService {

    private $adminDAO;

    public function __construct() {
        $this->adminDAO = new AdminDAO();
    }

    public function salvar($admin) {

        $conexao = Conexao::getConexao();
        $conexao->beginTransaction();

        try {

            $admin->setSenha(Seguranca::criptografaSenha($admin->getSenha()));

            if($admin->getUsuarioID() <= 0){

                if($this->exitePorEmail($admin->getEmail()))
                    throw new Exception("Erro, ja existe um Admin com esse Email!!!");
                if($this->exitePorLogin($admin->getLogin()))
                    throw new Exception("Erro, ja existe um Admin com esse Login!!!");

            }

            $admin = $this->adminDAO->salvar($admin);
            
            if(!strpos($admin->getMedia()->getCaminho(), Constantes::$IMG_ADM_DEFAULT)){
                Util::geraThumb($admin->getMedia()->getCaminho(), Constantes::$TAM_THUMB_IMG);
            }

            $conexao->commit();
            
            return $admin;

        } catch (Exception $err) {
            $conexao->rollBack();
            throw new Exception($err->getMessage());
        }
    }

    public function excluir($adminID) {

        try {
            if (!isset($adminID) || $adminID <= 0)
                throw new Exception("Codigo do Admin Nao Econtrado!!!");

            $this->adminDAO->excluir($adminID);
        } catch (Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function listar($pagina = 0, $email = "", $login = "", $statusID = -1, $carregarDependencias = false) {
        try {
            return $this->adminDAO->listar($pagina, $email, $login, $statusID, $carregarDependencias);
        } catch (Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function listarPorOrdem($pagina = 0, $tamanho = 0, $ordem = "adminID", $direcao = "DESC", $carregarDependencias = false) {
        try {
            return $this->adminDAO->listarPorOrdem($pagina, $tamanho, $ordem, $direcao, $carregarDependencias);
        } catch (Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function buscarPorID($adminID, $carregarDependencias = false) {
        try {
            if (!isset($adminID) || $adminID <= 0)
                throw new Exception("Codigo do Admin Nao Econtrado!!!");
            return $this->adminDAO->buscarPorID($adminID, $carregarDependencias);
        } catch (Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function total($email = "", $login = "", $statusID = -1) {
        try {
            return $this->adminDAO->total($email, $login, $statusID);
        } catch (Exception $err) {
            throw new Exception($err->getMessage());
        }
    }

    public function exitePorEmail($email){
        if($this->total($email) > 0) return true;
        return false;
    }

    public function exitePorLogin($login){
        if($this->total("",$login) > 0) return true;
        return false;
    }

}
?>